package com.financing.platform.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.mail.dto.MailContentDTO;
import com.financing.mail.service.MailSenderService;
import com.financing.platform.dao.IFaUserDao;
import com.financing.platform.dao.IMessageDao;
import com.financing.platform.dao.IMessageMailDao;
import com.financing.platform.dto.MessageDTO;
import com.financing.platform.dto.MessageMailDTO;
import com.financing.platform.service.IEmailTemplateService;
import com.financing.platform.util.DateUtil;
import com.financing.platform.util.Dict;
import com.financing.platform.util.StringUtil;
import com.financing.platform.vo.EmailTemplateUserVO;

/**
 * 
* @ClassName: FaVerifyMailJob 
* @Description: 
* @author chenming 
* @date 2014年9月22日 下午6:32:23 
*
 */
@Component(value="faVerifyMailJob" )
public class FaVerifyMailJob {
	
	Logger log = Logger.getLogger(this.getClass());
	
	@Value("#{settings['faVerifyMailList']}")
	private String faVerifyMailAddrs;
	
	@Autowired
	private IFaUserDao faUserDaoImpl;
	
	@Resource
	private MailSenderService mailSenderService;
	
	@Autowired
	private IEmailTemplateService emailTemplateServiceImpl;
	
	@Autowired
	private IMessageMailDao messageMailDaoImpl;
	
	@Autowired
	private IMessageDao messageDaoImpl;
	
//	@Scheduled(cron = "0 0 06 * * ?") 
	public void faVerifyNeed(){
		
		try{
			List<EmailTemplateUserVO> list = faUserDaoImpl.queryFaUserVerify();
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("time", DateUtil.getPerDay());
			param.put("count", list.size());
			param.put("list", list);
			param = emailTemplateServiceImpl.spliceEmailContent(Dict.mail_template_fa_need_verify, param);
			
			if(!param.isEmpty() && param.size() >0){
				MessageDTO messageDTO = new MessageDTO();
				MessageMailDTO messageMailDTO = new MessageMailDTO();
				
				messageDTO.setMsgTitle((String)param.get(Dict.mail_subject));
				messageDTO.setMsgContent((String)param.get(Dict.mail_content));
				messageDTO.setMsgType(Dict.message_type_fa);
				messageDTO.setStatus(Dict.status_normal);
				messageDTO.setCreateBy("system");
				messageDTO.setCreateDate(new Date());
				messageDTO.setUpdateBy("system");
				messageDTO.setUpdateDate(new Date());;
				messageMailDTO.setFpMailMessageId(messageDaoImpl.addMessage(messageDTO));
				messageMailDTO.setFpMailContent((String)param.get(Dict.mail_content));
				messageMailDTO.setFpMailTitle((String)param.get(Dict.mail_subject));
				messageMailDTO.setFpMailType(Dict.mail_fa_need_verify);
				messageMailDTO.setFpMailSendType(Dict.message_type_now);
				messageMailDTO.setFpMailSendTimes(new Integer(0));
				messageMailDTO.setStatus(Dict.status_normal);
				messageMailDTO.setFpMailSendStatus(Dict.message_status_not_send);
				messageMailDTO.setCreateBy(messageDTO.getUpdateBy());
				messageMailDTO.setUpdateBy(messageDTO.getUpdateBy());
				messageMailDTO.setCreateDate(messageDTO.getUpdateDate());
				messageMailDTO.setUpdateDate(messageDTO.getUpdateDate());
				
				String[] addrs = StringUtil.preEmailAddr(faVerifyMailAddrs);
				for(String addr:addrs){
					messageMailDTO.setFpMailTo(addr);
					messageMailDaoImpl.addMessageMail(messageMailDTO);
				}
			}
			
		}catch(Exception e){
			log.error("FA verify Mail error ",e);
			MailContentDTO mail = new MailContentDTO();
			mail.setRecAddress("chenming@superfa.cn");
			mail.setSubject("生产FA审核邮件异常");
			mail.setContext("生产FA审核邮件异常"+e.getMessage()+e);
			mailSenderService.send(mail);
		}
		
		
	}
	
	
}
